package com.bzkj.web.controller.ordermanagement.outbound;

import com.alibaba.fastjson2.JSON;
import com.bzkj.common.annotation.LogB;
import com.bzkj.common.core.controller.BaseController;
import com.bzkj.common.core.domain.AjaxResult;
import com.bzkj.common.core.page.TableDataInfo;
import com.bzkj.common.core.text.Convert;
import com.bzkj.common.domain.NccRequestDTO;
import com.bzkj.common.enums.BusinessType;
import com.bzkj.common.utils.ServletUtils;
import com.bzkj.document.domain.DocConfirmDTO;
import com.bzkj.document.domain.whstransbill.dto.WhsTransBillRequestBody;
import com.bzkj.document.domain.whstransbill.dto.WhsTransQueryDTO;
import com.bzkj.document.service.whstransbill.WhstransbillService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

import static com.bzkj.common.core.page.TableSupport.PAGE_NUM;
import static com.bzkj.common.core.page.TableSupport.PAGE_SIZE;

/**
 * @projectName: prt-wms
 * @package: com.bzkj.web.controller.ordermanagement.outbound
 * @className: TransferOrdersController
 * @description: 转库单控制层
 * @author: C1309
 * @createDate: 2024年12月10日
 * @version: v1.0
 */
@RestController
@RequestMapping("/transfer-orders")
public class TransferOrdersController extends BaseController {

    @Resource
    private WhstransbillService whstransbillService;

    /**
     * 查询NCC转库单
     *
     * @return 查询结果
     */
    @GetMapping("/ncc")
    @PreAuthorize("@ss.hasPermi('order:transfer:list')")
    public TableDataInfo selectNccTransferOrders(@RequestParam Map<String, Object> requestBody) {
        NccRequestDTO nccRequestDTO = getNccRequestDTO(requestBody);
        if (nccRequestDTO.getFbillflag() == null || nccRequestDTO.getFbillflag().isEmpty()) {
            nccRequestDTO.setFbillflag("1");
        }
        return whstransbillService.selectNccTransferOrders(nccRequestDTO);
    }

    /**
     * 确认ncc转库单
     *
     * @param requestBody 请求体
     * @return 确认结果
     */
    @PostMapping("/confirm")
    @PreAuthorize("@ss.hasPermi('order:transfer:confirm')")
    @LogB(title = "转库单", businessType = BusinessType.CONFIRM)
    public AjaxResult insertTransferOrders(@RequestBody DocConfirmDTO requestBody) {
        requestBody.setLoginUser(getLoginUser());
        return whstransbillService.confirmNccTransBill(requestBody);
    }

    /**
     * 查询本地转库单
     *
     * @param whsTransQueryJson 查询条件
     * @return 查询结果
     */
    @GetMapping
    @PreAuthorize("@ss.hasPermi('order:transfer:list')")
    public TableDataInfo selectLocalTransferOrders(WhsTransQueryDTO whsTransQueryJson) {
        if (whsTransQueryJson == null) {
            whsTransQueryJson = new WhsTransQueryDTO();
        }
        Integer pageNum = Convert.toInt(ServletUtils.getParameter(PAGE_NUM), 1);
        Integer pageSize = Convert.toInt(ServletUtils.getParameter(PAGE_SIZE), 10);
        return whstransbillService.selectLocalTransferOrders(whsTransQueryJson, pageNum, pageSize);
    }

    /**
     * 新增转库单
     *
     * @param requestBodyStr 请求数据
     * @return 结果
     */
    @PostMapping
    @PreAuthorize("@ss.hasPermi('order:transfer:add')")
    @LogB(title = "转库单", businessType = BusinessType.INSERT)
    public AjaxResult addTransferOrders(@RequestBody String requestBodyStr) {
        WhsTransBillRequestBody whsTransBillRequestBody = JSON.parseObject(requestBodyStr, WhsTransBillRequestBody.class);
        whsTransBillRequestBody.setLoginUser(getLoginUser());
        return whstransbillService.addTransferOrders(whsTransBillRequestBody);
    }

    /**
     * 转库出库单签字
     *
     * @param vbillcode 单据表头主键
     * @return 查询结果
     */
    @PutMapping("/{vbillcode}")
    @PreAuthorize("@ss.hasPermi('order:transfer:audit')")
    @LogB(title = "转库单", businessType = BusinessType.SIGN)
    public AjaxResult signatureWhsTransBound(@PathVariable("vbillcode") String vbillcode) {
        return whstransbillService.signatureWhsTransOutBound(vbillcode, getLoginUser());
    }

    /**
     * 修改转库出库单据
     *
     * @param responseBody 请求体
     * @return 修改结果
     */
    @PostMapping("/update")
    @PreAuthorize("@ss.hasPermi('order:transfer:edit')")
    @LogB(title = "转库单", businessType = BusinessType.UPDATE)
    public AjaxResult updateWhsTransBoundOrder(@RequestBody HashMap<String, Object> responseBody) {
        return whstransbillService.updateWhsTransBoundOrder(responseBody, getLoginUser(), false);
    }

    /**
     * 转库库单回退
     *
     * @param headerId 需要打回的单据类型
     * @return 回退结果
     */
    @PostMapping("/back/{headerid}")
    @PreAuthorize("@ss.hasPermi('order:transfer:edit')")
    @LogB(title = "转库单", businessType = BusinessType.CANCELED)
    public AjaxResult addPurchase(@PathVariable("headerid") String headerId) {
        return whstransbillService.orderBack(headerId, getLoginUser());
    }

    /**
     * 失败库存处理
     * @param headerId 表头主键
     * @return 处理结果
     */
    @PostMapping("/inventory/{headerid}")
    @LogB(title = "转库单", businessType = BusinessType.INVENTORY)
    @PreAuthorize("@ss.hasPermi('order:transfer:audit')")
    public AjaxResult updateInventory(@PathVariable("headerid") String headerId){
        return whstransbillService.updateInventory(headerId, getLoginUser());
    }

    /**
     * 删除转库单
     *
     * @param headerId 表头主键
     * @param reason 删除原因
     * @return 处理结果
     */
    @DeleteMapping("/{headerid}")
    @PreAuthorize("@ss.hasPermi('order:transfer:delete')")
    @LogB(title = "转库单", businessType = BusinessType.INVALID)
    public AjaxResult deleteTransferOrders(@PathVariable("headerid") String headerId,
                                           @RequestParam(name = "reason", defaultValue = "") String reason) {
        if (reason == null || reason.trim().isEmpty()) {
            return AjaxResult.error("作废原因似乎没有填写，请填写作废原因后重试！");
        }
        return whstransbillService.deleteTransferOrders(headerId, reason, getLoginUser());
    }
}
